﻿using AutoMapper;
using OpenIddict.Abstractions;
using OpenIddict.EntityFrameworkCore.Models;
using WT.OpenIddict.Application.Contracts.OpenIddictApplication;
using WT.OpenIddict.Application.Contracts.OpenIddictApplication.Dto;

namespace WT.OpenIddict.Application.OpenIddictApplication;

public class OpenIddictApplicationAppService : OpenIddictAppService, IOpenIddictApplicationAppService
{
    private readonly IOpenIddictApplicationManager _manager;


    public OpenIddictApplicationAppService(IOpenIddictApplicationManager manager, IMapper objectMapper)
    {
        _manager = manager;
        ObjectMapper = objectMapper;
    }

    public IMapper ObjectMapper { get; set; }


    public async Task<OpenIddictApplicationDto> GetClientByClientId(string clientId)
    {
        var client =
            (OpenIddictEntityFrameworkCoreApplication<Guid>)await _manager.FindByClientIdAsync(clientId);
        var clientInfo =
            ObjectMapper.Map<OpenIddictEntityFrameworkCoreApplication<Guid>, OpenIddictApplicationDto>(client);

        #region 获取附件，极简时去除，前端用文字头像代替

        // var file = await _fileRepo.FindAsync(it => it.BusinessId == clientInfo.Id.ToString());
        // if (file != null)
        // {
        //     string tempUrl = string.Empty;
        //     try
        //     {
        //         tempUrl = await _aliyunOssManager.GetTempUrlAsync(file.ObjectName);
        //     }
        //     catch (Exception ex)
        //     {
        //         tempUrl = file.Url;
        //     }
        //
        //     clientInfo.Logo = tempUrl;
        // }

        #endregion


        return clientInfo;
    }
}